// 比较版本号的函数
function compareVersions(v1, v2) {
	if (typeof v1 === 'string') {
		v1 = v1.split('.').map(Number);
	}
	if (typeof v2 === 'string') {
		v2 = v2.split('.').map(Number);
	}

	const len = Math.max(v1.length, v2.length);

	for (let i = 0; i < len; i++) {
		const num1 = v1[i] || 0;
		const num2 = v2[i] || 0;
		if (num1 > num2) {
			return 1;
		} else if (num1 < num2) {
			return -1;
		}
	}

	return 0;
}
// 获取版本号对比是否需要更新
const getAppVersion = (serveAppVersion = null, serveAppUrl = null) => {
	// 当前本地的PP版本号
	let currentAppVersion = null
	plus.runtime.getProperty(plus.runtime.appid, function(e) {
		currentAppVersion = e.version
		if (compareVersions(serveAppVersion, currentAppVersion) > 0) {
			uni.showModal({
				content: '当前有最新版本，是否要下载更新？',
				success: (res) => {
					if (res.confirm) {
						plus.runtime.openURL(serveAppUrl);
					} else {
						// plus.runtime.quit();
					}
				}
			})
		}
	});
}

export default getAppVersion